﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;

namespace Ewts.ashx
{
    /// <summary>
    /// IsGoodCode 的摘要说明
    /// </summary>
    public class IsGoodCode : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string goodsname = "";
            string isHw = "";
            string storageCode = "";

            //2013-02-27  梁玲
            //验证称重页面的货物接收、分类入库页面的分类汇总及入库
            //0:验证物品编码，废物编码；1：验证物品编码，废物编码，库位编码
            string checkStatus = "";
            if (context.Request["goodCodeid"] != null && context.Request["goodCodeid"].ToString().Trim() != "")
            {
                goodsname = context.Request["goodCodeid"].ToString().Trim();

            }
            if (context.Request["isHw"] != null && context.Request["isHw"].ToString().Trim() != "")
            {
                isHw = context.Request["isHw"].ToString().Trim();

            }
            if (context.Request["sid"] != null && context.Request["sid"].ToString().Trim() != "")
            {
                storageCode = context.Request["sid"].ToString().Trim();

            }
            if (context.Request["checkStatus"] != null && context.Request["checkStatus"].ToString().Trim() != "")
            {
                checkStatus = context.Request["checkStatus"].ToString().Trim();

            }
            string result = "";
            if (checkStatus == "0")
            {
                result = CheckClassifyInsertByCode(goodsname, isHw);
            }
            else
            {
                result = CheckInsertRoomByCode(goodsname, isHw, storageCode);
            }
            context.Response.Write(result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 验证验证物品编码，废物编码
        /// </summary>
        /// <param name="goodsname">物品编码</param>
        /// <param name="isHw">废物编码</param>
        /// <returns>0：存在,1：物品编码不合法，2：废物编码不合法</returns>
        protected string CheckClassifyInsertByCode(string goodsname, string isHw)
        {
            DataTable dt = new DataTable();

            DataLib.Model.Goods model = new DataLib.Model.Goods();
            string result = "-1";

            ///2013-02-27 梁玲
            ///判断物品编码是否存在，0表示存在，1表示不合法
            string where = "";
            where = "goodsName='" + goodsname + "'";
            dt = new DataLib.BLL.Goods().GetList(where).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                result = "0";
            }
            else
            {
                result = "1";
                return result;
            }
            ///2013-02-27 梁玲
            ///判断废物编码是否存在，0表示存在，2表示不合法
            if (isHw != null && isHw.Trim() != "")      //如果废弃物编码为空，则不验证
            {
                where = "itemCode='" + isHw + "'";
                dt = null;
                dt = new DataLib.BLL.Rubbish().GetList(where).Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    result = "0";
                }
                else
                {
                    result = "2";
                    return result;
                }
            }
            return result;
        }
        /// <summary>
        /// 验证物品编码，废物编码，库位编码
        /// </summary>
        /// <param name="goodsname">物品编码</param>
        /// <param name="isHw">废物编码</param>
        /// <param name="storageCode">库位编码</param>
        /// <returns>0：存在,1：物品编码不合法，2：废物编码不合法,3：库位不合法</returns>
        protected string CheckInsertRoomByCode(string goodsname, string isHw, string storageCode)
        {
            DataTable dt = new DataTable();

            DataLib.Model.Goods model = new DataLib.Model.Goods();
            string result = "-1";

            ///2013-02-27 梁玲
            ///判断物品编码是否存在，0表示存在，1表示不合法
            string where = "";
            where = "goodsName='" + goodsname + "'";
            dt = new DataLib.BLL.Goods().GetList(where).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                result = "0";
            }
            else
            {
                result = "1";
                return result;
            }
            ///2013-02-27 梁玲
            ///判断废物编码是否存在，0表示存在，2表示不合法
            if (isHw != null && isHw.Trim() != "")      //如果废弃物编码为空，则不验证
            {
                where = "itemCode='" + isHw + "'";
                dt = null;
                dt = new DataLib.BLL.Rubbish().GetList(where).Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    result = "0";
                }
                else
                {
                    result = "2";
                    return result;
                }
            }
            ///2013-02-27 梁玲
            ///判断库位编码是否存在，0表示存在，3表示不合法
            where = "storagecode='" + storageCode + "'";
            dt = new DataTable();
            dt = new DataLib.BLL.StoragePostion().GetList(where).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                result = "0";
            }
            else
            {
                result = "3";
            }
            return result;
        }
    }
}